layui.use(['form', 'layer'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer;
	var arr2 = new Array();
	var danweim = '';
	$("#loginA").click(function() {
		$(this).addClass("bord");
		$("#resA").removeClass("bord");
	});
	$("#resA").click(function() {
		$(this).addClass("bord");
		$("#loginA").removeClass("bord");
	});

	$(".loginBody .seraph").click(function() {
		layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧", {
			time: 5000
		});
	})
	/*记录账户信息*/
	var access = false;
	var ls = 0;
	form.on('radio(setacc)', function(data) {
		var selche = document.getElementById('accon');
		ls++;
		if(ls % 2 == 1) {
			selche.checked = true;
			access = true;
		} else {
			selche.checked = false;
			access = false;
		}

		form.render();

	});

	if(document.cookie) {
		var cook = document.cookie.split(',');
		try {
			var arr1 = cook[0].split('=');
			var arr2 = cook[1].split("=");
			$('#loginName').val(arr1[1]);
			$('#loginPass').val(arr2[1]);
		} catch(e) {
			//TODO handle the exception
		}

	} else {
	}

	/* 登录按钮*/
	$('#loginA').click(function() {
		$('#loginHtml').css('display', 'block');
		$('#resHtml').css('display', 'none');
	});

	/*注册按钮*/
	$('#resA').click(function() {
		$('#loginHtml').css('display', 'none');
		$('#resHtml').css('display', 'block');
		//	getallent();
	});

	/*注册用户*/

	$('#res').click(function() {
		adduser();
	});

	function adduser() {
		var resName = $.trim($('#resName').val());
		var resPass = $.trim($('#resPass').val());
		var resphon = $.trim($('#resphon').val());
		var resclass = $('#resclass').val();
		var userNo = $.trim($('#userNo').val());
		var res = getval();
		resclass = 10;

		if(res.length <= 1) {
			if(resName != '' && resPass != '' && resphon.length == 11) {
				$.ajax({
					url: url + 'baseUsers/register',
					dataType: 'json',
					async: false,
					type: "POST",
					data: {
						username: resName,
						password: resPass,
						phone: resphon,
						entId: res.toString(),
						userNo: userNo
					},
					success: function(data) {
						if(data.code == 0) {
							sessionStorage.setItem('usersalt', data.data);
							sessionStorage.setItem('username', resName);

							layer.confirm('注册成功！', {
								btn: ['确定'] //按钮
							}, function() {
								layer.closeAll();
								setTimeout('location.href="main.html"');
							}, function() {

							});

						} else {
							layer.msg(data.msg);
						}

					},
					error: function(error) {
					}
				});
			} else {
				layer.msg('输入信息有误');
			}
		} else {
			layer.msg('只能选择一个单位');
		}

	}

	/*登录*/

	$('#login').click(function() {
		logouser();
	});

	function logouser() {

		var loginName = $('#loginName').val();
		var loginPass = $('#loginPass').val();
		$.ajax({
			url: url + 'baseUsers/login',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				username: loginName,
				password: loginPass
			},
			success: function(data) {
				if(data.code == 0) {
					sessionStorage.setItem('usersalt', data.data);
					sessionStorage.setItem('username', loginName);
					layer.msg('登录成功');
					if(access == true) {
						var Days = 30;
						var exp = new Date();
						exp.setTime(exp.getTime() + Days * 24 * 60 * 1000);

						document.cookie = 'name=' + loginName + ",pass=" + loginPass + ";expires=" + exp.toGMTString();
						//						alert(document.cookie);
					} else {
						var exp = new Date();
						exp.setTime(exp.getTime() - 1);
						var cval = document.cookie;
						if(cval != null)
							document.cookie = 'name=' + loginName + ",pass=" + loginPass + ";expires=" + exp.toGMTString();

					}
					setTimeout('window.location.href = "main.html";', 2000);
				} else {
					layer.msg(data.msg);
				}

			},
			error: function(error) {
			}
		});

	}

	/*忘记*/
	$('#forget').click(function() {
		layer.confirm('请联系管理员', {
			btn: ['确定'] //按钮
		}, function() {
			layer.closeAll();

		}, function() {

		});
	});

	///baseEnt/getAllEnt
	/*获取所有单位*/
	//function getallent(){
	//	
	//	$.ajax({
	//		url: url+'baseEnt/getAllEnt',
	//    		dataType: 'json',
	//    		async: false,
	//    		type: "POST",
	//    		data:{
	//    			
	//    		},
	//    		success: function(data) {
	//    			$('#resclass').empty();
	//    			if(data.code==0){
	//    				var list=data.data;
	//    				var opt='<li >公司名</li><li ><input lay-filter="selchecked" type="checkbox" name="checkall" id="selcheck" />全选</li>';
	//    				for(var i=0;i<list.length;i++){
	//    					opt+='<li value="'+list[i].id+'" ><input type="checkbox" name="checkname" ><span>'+list[i].name+'</span></option>'
	//    				}
	//    				$('#resclass').append(opt);
	//    				form.render();
	//    			}
	//    			
	//
	//    		},
	//    		error: function(error) {
	//    		}
	//    	});
	//	
	//}

	//layui.tree({
	//elem: '#resclass'
	//,nodes: [{ //节点数据
	//  name: '节点A'
	//  ,children: [{
	//    name: '节点A1'
	//  }]
	//}, {
	//  name: '节点B'
	//  ,children: [{
	//    name: '节点B1'
	//    ,alias: 'bb' //可选
	//    ,id: '123' //可选
	//  }, {
	//    name: '节点B2'
	//  }]
	//}] 
	//,click: function(node){
	//}  
	//});
	//form.render();
	//表单输入效果
	//  $(".loginBody .input-item").click(function(e){
	//      e.stopPropagation();
	//      $(this).addClass("layui-input-focus").find(".layui-input").focus();
	//  })
	//  $(".loginBody .layui-form-item .layui-input").focus(function(){
	//      $(this).parent().addClass("layui-input-focus");
	//  })
	//  $(".loginBody .layui-form-item .layui-input").blur(function(){
	//      $(this).parent().removeClass("layui-input-focus");
	//      if($(this).val() != ''){
	//          $(this).parent().addClass("layui-input-active");
	//      }else{
	//          $(this).parent().removeClass("layui-input-active");
	//      }

	$('#resclas').click(function() {
		$('#resclass').show();
		$('#resclas').val('')
		$.fn.zTree.init($("#resclass"), setting, zNodes);
	});

	var company;

	getallent();

	function getallent() {

		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {

			},
			success: function(data) {
				$('#resclass').empty();
				if(data.code == 0) {
					var list = data.data;
					company = list;

				}

			},
			error: function(error) {
			}
		});

	}

	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		check: {
			enable: true,
			chkStyle: "radio", //单选框
			radioType: "all" //对所有节点设置单选
		},
		callback: {
			onCheck: zTreeOnCheck
		}

	};

	var zNodes = company;

	$.fn.zTree.init($("#resclass"), setting, zNodes);

	$('#getval').click(function() {
		getval();
	})

	function zTreeOnCheck(event, treeId, treeNode) {
		var checked = treeNode.checked;
		danweim = treeNode ? treeNode.name : "root"
		$('#resclas').val(danweim)
		$('#resclass').hide();

	};

	function getval() {
		arr2 = [];
		danweim = '';
		var treeObj = $.fn.zTree.getZTreeObj("resclass");
		var nodes = treeObj.getCheckedNodes(true),

			v = "";
		for(var i = 0; i < nodes.length; i++) {
			v += nodes[i].name + ",";

			arr2.push(nodes[i].id); //获取选中节点的值
		}
		return arr2;
	}

});